package com.rc.evcharger.service.member;

import com.rc.evcharger.mapper.member.MemberAccountMapper;
import com.rc.evcharger.mapper.member.MemberRefundOrderMapper;
import com.rc.evcharger.mapper.member.MemberRefundRecordMapper;
import com.rc.evcharger.model.member.MemberAccount;
import com.rc.evcharger.model.member.MemberAccountRecord;
import com.rc.evcharger.model.member.MemberRefundOrder;
import com.rc.evcharger.model.member.MemberRefundRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Create by Yang on 2021/11/9
 */
@Service
public class MemberRefundOrderServiceImpl implements MemberRefundOrderService {

    @Autowired
    private MemberRefundOrderMapper memberRefundOrderMapper;

    @Autowired
    private MemberRefundRecordMapper memberRefundRecordMapper;

    @Autowired
    private MemberAccountMapper memberAccountMapper;

    @Override
    public MemberRefundOrderMapper getMapper() {
        return memberRefundOrderMapper;
    }

    @Override
    public int updateByMemberAccountRecord(MemberAccountRecord memberAccountRecord) {
        //扣减总额度
        BigDecimal remaiAmount = memberAccountRecord.getAmount();
        if (remaiAmount.compareTo(BigDecimal.ZERO) == 0) {
            return 0;
        }

        /**
         *  扣减规则
         *  1.总金额-新额度=旧额度
         *  2.订单金额-旧额度=扣款额度
         *  3.如果扣款额度为负数，将不会扣减额度
         */
        //新额度
        BigDecimal refundAmount = memberRefundOrderMapper.countRefundAmount(memberAccountRecord.getMemberId());
        //总金额
        MemberAccount memberAccount = memberAccountMapper.selectByMemberIdForceMaster(memberAccountRecord.getMemberId());
        //获取旧额度
        BigDecimal oldRefundAmount = memberAccount.getAccount().subtract(refundAmount);
        //扣款额度小于0不执行
        remaiAmount = remaiAmount.subtract(oldRefundAmount);
        if (remaiAmount.compareTo(BigDecimal.ZERO) <= 0) {
            return 0;
        }

        //先执行后判断
        do {
            //查找距今最长时间的记录,并且有效记录
            MemberRefundOrder memberRefundOrder = memberRefundOrderMapper.selectByMemberId(memberAccountRecord.getMemberId());
            if (memberRefundOrder == null) {
                return 0;
            }

            //添加额度扣减记录
            MemberRefundRecord memberRefundRecord = new MemberRefundRecord();
            memberRefundRecord.setTenantCode(memberAccountRecord.getTenantCode());
            memberRefundRecord.setMemberId(memberAccountRecord.getMemberId());     //会员ID
            memberRefundRecord.setAccountType(memberAccountRecord.getAccountType()); //扣款类型
            memberRefundRecord.setRecordType("0");  //扣减记录
            memberRefundRecord.setOrderNum(memberAccountRecord.getLocalTradeNo()); //订单号
            memberRefundRecord.setCreatedTime(new Date());
            memberRefundRecord.setRefundOrderId(memberRefundOrder.getRefundOrderId());

            //保留初始值
            BigDecimal refundFactAmount = memberRefundOrder.getRefundFactAmount();
            BigDecimal refundGiveAmount = memberRefundOrder.getRefundGiveAmount();
            /**
             * 消耗充值额度
             * 如果变动余额>充值额度  耗尽充值额度，继续下一步消耗赠送余额
             * 如果变动余额<充值额度  保存充值额度，结束返回
             * 如果变动余额=充值额度  保存充值额度，结束返回
             *
             * 消耗赠送额度
             * 如果变动余额>赠送额度  耗尽赠送额度，完成额度变更后，继续循环消耗
             * 如果变动余额<赠送额度  保存赠送额度，结束返回
             * 如果变动余额=赠送额度  保存赠送额度，完成额度变更，结束返回
             */
            if ((remaiAmount.subtract(memberRefundOrder.getRefundFactAmount())).compareTo(BigDecimal.ZERO) > 0) {

                memberRefundOrder.setRefundedFactAmount(memberRefundOrder.getRefundFactAmount());//已退金额
                memberRefundOrder.setRefundFactAmount(BigDecimal.ZERO); //额度耗尽

                memberRefundOrder.setLastUpdatedTime(new Date());

                memberRefundRecord.setFactAmount(refundFactAmount);

                //额度减少
                remaiAmount = remaiAmount.subtract(refundFactAmount);
            } else {


                //变更已退的额度
                memberRefundOrder.setRefundedFactAmount(memberRefundOrder.getRefundedFactAmount().add(remaiAmount));
                //变更可退的额度
                memberRefundOrder.setRefundFactAmount(memberRefundOrder.getRefundFactAmount().subtract(remaiAmount));

                memberRefundOrder.setLastUpdatedTime(new Date());

                //更新额度
                memberRefundOrderMapper.updateByPrimaryKeySelective(memberRefundOrder);

                //添加变更记录
                memberRefundRecord.setFactAmount(remaiAmount);
                memberRefundRecord.setResultFactAmount(memberRefundOrder.getRefundFactAmount());
                memberRefundRecord.setGiveAmount(BigDecimal.ZERO);
                memberRefundRecord.setResultGiveAmount(memberRefundOrder.getRefundGiveAmount());
                memberRefundRecordMapper.insertSelective(memberRefundRecord);

                //变动余额耗尽后，直接返回
                break;

            }

            //消耗赠送额度
            if (remaiAmount.subtract(memberRefundOrder.getRefundGiveAmount()).compareTo(BigDecimal.ZERO) > 0) {

                //添加变更记录
                memberRefundRecord.setFactAmount(refundFactAmount);
                memberRefundRecord.setResultFactAmount(BigDecimal.ZERO);
                memberRefundRecord.setGiveAmount(refundGiveAmount);
                memberRefundRecord.setResultGiveAmount(BigDecimal.ZERO);
                memberRefundRecordMapper.insertSelective(memberRefundRecord);


                memberRefundOrder.setRefundFactAmount(BigDecimal.ZERO);
                memberRefundOrder.setLastUpdatedTime(new Date());
                memberRefundOrder.setRefundStatus("4"); //额度耗尽
                memberRefundOrder.setFinishTime(new Date());
                //更新额度
                memberRefundOrderMapper.updateByPrimaryKeySelective(memberRefundOrder);

                //额度减少
                remaiAmount = remaiAmount.subtract(refundGiveAmount);

            } else if (remaiAmount.subtract(memberRefundOrder.getRefundGiveAmount()).compareTo(BigDecimal.ZERO) == 0) {

                //添加变更记录
                memberRefundRecord.setFactAmount(refundFactAmount);
                memberRefundRecord.setResultFactAmount(BigDecimal.ZERO);
                memberRefundRecord.setGiveAmount(refundGiveAmount);
                memberRefundRecord.setResultGiveAmount(BigDecimal.ZERO);
                memberRefundRecordMapper.insertSelective(memberRefundRecord);


                memberRefundOrder.setRefundFactAmount(BigDecimal.ZERO);
                memberRefundOrder.setLastUpdatedTime(new Date());
                memberRefundOrder.setRefundStatus("4"); //额度耗尽
                memberRefundOrder.setFinishTime(new Date());

                //更新额度
                memberRefundOrderMapper.updateByPrimaryKeySelective(memberRefundOrder);
                break;
            } else if (remaiAmount.subtract(memberRefundOrder.getRefundGiveAmount()).compareTo(BigDecimal.ZERO) < 0) {

                //添加变更记录
                memberRefundRecord.setFactAmount(refundFactAmount);
                memberRefundRecord.setResultFactAmount(BigDecimal.ZERO);
                memberRefundRecord.setGiveAmount(remaiAmount);
                memberRefundRecord.setResultGiveAmount(memberRefundOrder.getRefundGiveAmount().subtract(remaiAmount));
                memberRefundRecordMapper.insertSelective(memberRefundRecord);


                memberRefundOrder.setRefundGiveAmount(memberRefundOrder.getRefundGiveAmount().subtract(remaiAmount));
                memberRefundOrder.setLastUpdatedTime(new Date());

                //更新额度
                memberRefundOrderMapper.updateByPrimaryKeySelective(memberRefundOrder);
                break;
            }


            //判断每次扣减额度是否充足
        } while (remaiAmount.compareTo(BigDecimal.ZERO) > 0);


        return 1;
    }
}
